/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.Date;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import model.entity.Bill;
import model.entity.Product;
import model.entity.BillDetail;

/**
 *
 * @author KAKAIT
 */
public class BillImp {
    
    
    public boolean addbill(Bill bil )
    {
        boolean kq = true;
        try {
            PreparedStatement ps = ConnectDatabase.getConnection().prepareStatement("insert into bill values (?,?,?,?,?)");
            ps.setString(1, bil.getBillid());
            ps.setString(2, bil.getUsername());
            ps.setString(3, bil.getDatepurchase());
            ps.setDouble(4, bil.getTotalamount());
            ps.setBoolean(5, bil.isStatus());
            kq = ps.executeUpdate()>0;
        } catch (Exception e) {
            kq = false;
        }
        return kq;
    }
    
    
    public void addbilldetail(String billid, int productid, double amount, int quantity)
    {
        try {
            PreparedStatement ps = ConnectDatabase.getConnection().prepareStatement("insert into billdetail values (?,?,?,?)");
            ps.setString(1, billid);
            ps.setInt(2, productid);
            ps.setDouble(3, amount);
            ps.setInt(4, quantity);
            ps.executeUpdate();
            ps.close();
        } catch (Exception e) {
           e.printStackTrace();
        }
    }
    
    public List<BillDetail> ListBillDetail(String billid)
    {
        List<BillDetail> lbd = new ArrayList<BillDetail>();
        try {
            Connection con = ConnectDatabase.getConnection();
            PreparedStatement ps = con.prepareStatement("select * from billdetail where billid = ?");
            ps.setString(1, billid);
            ResultSet rs = ps.executeQuery();
            while(rs.next())
            {
                lbd.add(new BillDetail(billid, rs.getInt("productid"), rs.getDouble("amount"), rs.getInt("quantity")));
            }
        } catch (Exception e) {
            lbd = null;
        }
        return lbd;
        
    }
    
    public List<Bill> Listbillbyusername(String username)
    {
        List<Bill> lbil = new ArrayList<Bill>();
        try {
            
            Connection con = ConnectDatabase.getConnection();
            PreparedStatement ps = con.prepareStatement("select * from bill where username = ?");
            ps.setString(1, username);
            ResultSet rs = ps.executeQuery();
            while(rs.next())
            {
                lbil.add(new Bill(rs.getString("billid"), username, rs.getString("datepurchase"), rs.getDouble("totalamount"), rs.getBoolean("status")));
            }
        } catch (Exception e) {
            lbil = null;
        }
       return lbil;
    }
    
    public boolean checkbillid_username(String billid, String usrename)
    {
        boolean kq = true;
        try {
            Connection con = ConnectDatabase.getConnection();
            PreparedStatement ps = con.prepareStatement("select * from bill where billid = ? and usrename = ?");
            ps.setString(1, billid);
            ps.setString(2, usrename);
            kq = ps.executeUpdate()>0;
        } catch (Exception e) {
            kq = false;
        }
        return kq;
        
    }
    
    public List<Bill> AllBill()
    {
        List<Bill> lstbill = new ArrayList<Bill>();
        try {
            Statement st = ConnectDatabase.getConnection().createStatement();
            ResultSet rs = st.executeQuery("select * from bill order by billid desc");
            while(rs.next())
            {
                lstbill.add(new Bill(rs.getString("billid"), rs.getString("username"), rs.getString("datepurchase"), rs.getDouble("totalamount"), rs.getBoolean("status")));
            }
        } catch (Exception e) {
            lstbill = null;
        }
        return lstbill;
    }
    
    public Bill BillDetail(String billid)
    {
        Bill bill = new Bill();
        try {
            Connection con = ConnectDatabase.getConnection();
            PreparedStatement prst = con.prepareCall("select * from bill where billid = ?");
            prst.setString(1, billid);
            ResultSet rs = prst.executeQuery();
            while(rs.next())
            {
                bill.setBillid(rs.getString("billid"));
                bill.setUsername(rs.getString("username"));
                bill.setDatepurchase(rs.getString("datepurchase"));
                bill.setTotalamount(rs.getDouble("totalamount"));
                bill.setStatus(rs.getBoolean("status"));
            }
        } catch (Exception e) {
            bill = null;
        }
        return bill;
    }
    public boolean EditBill(String billid,String status)
    {
        boolean kq = true;
        try {
            Connection con = ConnectDatabase.getConnection();
            PreparedStatement prst = con.prepareCall("update bill set status = '"+ status+ "' where billid = ?");
            prst.setString(1, billid);
            prst.execute();
        } catch (Exception e) {
            kq = false;
        }
        return kq;
    }
    
    
}
